import java.util.HashSet;

public class Solution128 {
    public int longestConsecutive(int[] nums) {
        HashSet<Integer> set = new HashSet<>();
        for (int num : nums) {
            set.add(num);
        }
        int res = 0;
        for (Integer integer : set) {
            if (!set.contains(integer - 1)) {
                int num = integer + 1;
                while (set.contains(num)) {
                    num++;
                }
                res = Math.max(res, num - integer);
                if (res > set.size() / 2) {
                    break;
                }
            }
        }
        return res;
    }
}
